import CZML_GENERALTOOLS_CLASS from "@/common/CeMainControl/main/CZML_GENERALTOOLS_CLASS.js"
import CZML_Point from "@/common/CeMainControl/entity/CZML_Point"
import { useLayer } from "@/common/uses/use-layer"

let viewer // 地图对象
let layer
let Cesium
let ws

// 初始化当前业务
export function onMounted(mapInstance) {
  viewer = mapInstance // 记录map
  Cesium = DE.__namespace.Cesium
  const startTime = new Date(2023, 8, 18, 18, 26, 53)
  const endTime = new Date(2023, 8, 19, 18, 26, 53)
  CZML_GENERALTOOLS_CLASS.NewScenario(startTime.toISOString(), endTime.toISOString(), "仿真名字123", viewer)
  // const globeRotate = new DE.GlobeRotate(viewer)
  // globeRotate.start()
}

// 释放当前业务
export function onUnmounted() {
  viewer = null
  ws && ws.close()
  ws = null
}

export function showSatellite() {
  layer && viewer.removeLayer(layer)
  layer = useLayer(`czmlLayer${DE.Util.uuid()}`, {
    viewer,
    type: DE.LayerType.CZML,
    url: "testData/lowSatellite.czml"
  })
  viewer.flyToPosition({ lat: -20.236138, lng: -144.262661, alt: 41875827, heading: 339, pitch: -90 })
}

export function showCzmlPoint() {
  layer && viewer.removeLayer(layer)
  const point = new CZML_Point(viewer, "p1", "点1", [-111.0, 40.0, 0], [255, 255, 255, 255], [255, 0, 0, 255], 4, 20)
  point.AddPoint()
}


export function showWebSocketCzml() {
  ws && ws.close()
  layer && viewer.removeLayer(layer)
  layer = new DE.CzmlLayer("czmlLayer")
  ws = new WebSocket("ws://localhost:8000/socketTest")

  ws.onopen = function () {
    console.log("客户端连接成功")
  }

  ws.onmessage = function (e) {
    console.log("收到服务器响应", JSON.parse(e.data))
    layer.process(JSON.parse(e.data))
    // viewer.flyTo(layer)
  }
  // 连接关闭后的回调函数
  ws.onclose = function (evt) {
    console.log("关闭客户端连接")
  }

  // 连接失败后的回调函数
  ws.onerror = function (evt) {
    console.log("连接失败了")
  }

  viewer.addLayer(layer)
}
